package com.raincat.toolsdemo.vaccin;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceManager;

import com.raincat.toolsdemo.R;

/**
 * <pre>
 *     author : RainCat
 *     org    : Shenzhen JinYu Network Technology Co., Ltd.
 *     e-mail : nining377@gmail.com
 *     time   : 2020/11/05
 *     desc   : 说明
 *     version: 1.0
 * </pre>
 */

public class PersonalInfoActivity extends AppCompatActivity {
    private Context context;
    private SharedPreferences preferences;

    private EditText et_name, et_card, et_phone;
    private TextView tv_birthday;
    private RadioGroup rg_group;
    private Button btn_save;

    private String birthday = "";
    private int Ftime = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personalinfo_main);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        context = PersonalInfoActivity.this;
        preferences = PreferenceManager.getDefaultSharedPreferences(context);

        init();
        load();
        listener();
    }

    private void init() {
        et_name = findViewById(R.id.et_name);
        et_card = findViewById(R.id.et_card);
        et_phone = findViewById(R.id.et_phone);
        tv_birthday = findViewById(R.id.tv_birthday);
        rg_group = findViewById(R.id.rg_group);
        btn_save = findViewById(R.id.btn_save);
    }

    private void listener() {
        et_card.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String text = et_card.getText().toString().trim();
                if (text.length() == 18) {
                    String year = text.substring(6, 10);// 截取年
                    String month = text.substring(10, 12);// 截取月份
                    String day = text.substring(12, 14);// 截取天
                    tv_birthday.setText(String.format("生　日：%s-%s-%s", year, month, day));
                    birthday = String.format("%s-%s-%s", year, month, day);
                }
            }
        });

        rg_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int id) {
                if (id == R.id.rb_1)
                    Ftime = 1;
                else if (id == R.id.rb_2)
                    Ftime = 2;
                else if (id == R.id.rb_3)
                    Ftime = 3;
            }
        });

        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (check()) {
                    save();
                    Toast.makeText(context, "保存成功", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private boolean check() {
        EditText view = null;
        String error = "";
        boolean pass = true;
        if (et_name.getText().toString().trim().length() == 0) {
            error = "名字不能为空";
            view = et_name;
            pass = false;
        } else if (et_card.getText().toString().trim().length() == 0) {
            error = "身份证不能为空";
            view = et_card;
            pass = false;
        } else if (et_phone.getText().toString().trim().length() == 0) {
            error = "电话不能为空";
            view = et_phone;
            pass = false;
        } else if (et_card.getText().toString().trim().length() != 18) {
            error = "身份证不为18位";
            view = et_card;
            pass = false;
        } else if (et_phone.getText().toString().trim().length() != 11) {
            error = "电话不为11位";
            view = et_phone;
            pass = false;
        }

        if (!pass) {
            view.requestFocus();
            view.setError(error);
        }

        return pass;
    }

    private void load() {
        et_name.setText(preferences.getString("cname", ""));
        tv_birthday.setText("生　日：" + preferences.getString("birthday", ""));
        et_phone.setText(preferences.getString("tel", ""));
        et_card.setText(preferences.getString("idcard", ""));
        Ftime = preferences.getInt("Ftime", 1);
        if (Ftime == 1)
            rg_group.check(R.id.rb_1);
        else if (Ftime == 2)
            rg_group.check(R.id.rb_2);
        else if (Ftime == 3)
            rg_group.check(R.id.rb_3);
    }

    private void save() {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("cname", et_name.getText().toString());
        editor.putString("birthday", birthday);
        editor.putString("tel", et_phone.getText().toString());
        editor.putString("idcard", et_card.getText().toString());
        editor.putInt("Ftime", Ftime).apply();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
